package com.muqi.app.user.widget;

import java.util.List;

import com.easemob.easeui.widget.CircularImage;
import com.muqi.app.qlearn.adapter.AvatarSelectAdapter;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.db.MyClassStudent;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ClassWithStudentsWindow extends PopupWindow implements OnItemClickListener {
	private View conentView;
	private LinearLayout mClassview;
	private MyGridView mStudentView;
	private Activity mActivity;
	private List<MyClassStudent> studentLists;
	private AvatarSelectAdapter mAdapter = null;
	private SelectCWithSListener listener = null;
	private ChildBean currentChild = null;
	
	
	@SuppressLint("InflateParams")
	@SuppressWarnings("deprecation")
	public ClassWithStudentsWindow(final Activity context, ChildBean currentChild,
			List<MyClassStudent> studentLists,
			SelectCWithSListener listener) {
		this.mActivity = context;
		this.currentChild = currentChild;
		this.studentLists = studentLists;
		this.listener = listener;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.popup_classwithstudents, null);
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		this.setContentView(conentView);
		this.setWidth(w);
		this.setHeight(h);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.ActionSheetAnimation);
		
		mStudentView = (MyGridView) conentView.findViewById(R.id.myGridView);
		mClassview = (LinearLayout) conentView.findViewById(R.id.classLayout);
		
		drawMyCLassLayout();
		setCLassStudents();
	}
	
	/**
	 * showPopupWidow
	 * 
	 * @param parent
	 */
	@SuppressLint("InflateParams") 
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		} else {
			this.dismiss();
		}
	}
	
	@SuppressLint("InflateParams") 
	private void drawMyCLassLayout(){
		mClassview.removeAllViews();
		View convertView = LayoutInflater.from(mActivity).inflate(R.layout.item_user_avatr, null);
		CircularImage itemAvatar = (CircularImage) convertView.findViewById(R.id.mCircularImage);
		TextView itemName = (TextView) convertView.findViewById(R.id.mUserName);
		//ImageView isChecked = (ImageView) convertView.findViewById(R.id.mChecked);
		itemAvatar.setImageResource(R.drawable.tree_class_default);
		itemName.setText(currentChild.class_name+"");
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listener.onSelectResult(1, currentChild.class_name, null);
				ClassWithStudentsWindow.this.dismiss();
			}
		});
		mClassview.addView(convertView);
	}
	
	private void setCLassStudents(){
		if(mAdapter != null){
			mAdapter = null;
		}
		if(studentLists!=null && studentLists.size()>0){
			mAdapter = new AvatarSelectAdapter(mActivity, studentLists);
			mStudentView.setAdapter(mAdapter);
			mStudentView.setOnItemClickListener(this);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		for(int i=0; i<studentLists.size(); i++){
			if(i==position){
				studentLists.get(i).isChecked = true;
			}else{
				studentLists.get(i).isChecked = false;
			}
		}
		if (mAdapter != null)mAdapter.notifyDataSetChanged();
		listener.onSelectResult(2, currentChild.class_name, studentLists.get(position));
		ClassWithStudentsWindow.this.dismiss();
	}
	
	public interface SelectCWithSListener {
		void onSelectResult(int type, String className, MyClassStudent selected);
	}
}
